package cn.itcast.oa.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.UUID;

import org.apache.struts2.ServletActionContext;
import org.jbpm.api.ProcessDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.oa.base.ModelDrivenBaseAction;
import cn.itcast.oa.model.ApplicationTemplate;

@Controller
@Scope("prototype")
public class ApplicationTemplateAction extends ModelDrivenBaseAction<ApplicationTemplate> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private File upLoad;
	private InputStream inputStream;

	public String list() throws Exception {
		List<ApplicationTemplate> applicationTemplateList = applicationTemplateService.findAll();
		ActionContext.getContext().put("applicationTemplateList", applicationTemplateList);
		return "list";
	}

	public String delete() throws Exception {
		applicationTemplateService.delete(model.getId());
		return "tolist";
	}

	public String editUI() throws Exception {
		List<ProcessDefinition> processDefinitionList = processDefinitionService.findAllLatestVersions();
		ActionContext.getContext().put("processDefinitionList", processDefinitionList);

		ApplicationTemplate applicationTemplate = applicationTemplateService.getById(model.getId());
		ActionContext.getContext().getValueStack().push(applicationTemplate);
		return "saveUI";
	}

	public String edit() throws Exception {
		ApplicationTemplate applicationTemplate = applicationTemplateService.getById(model.getId());
		applicationTemplate.setName(model.getName());
		applicationTemplate.setProcessDefinitionKey(model.getProcessDefinitionKey());
		File file = new File(applicationTemplate.getPath());
		if (upLoad != null) {
			if (file.exists()) {
				file.delete();
			}
			String path=saveUploadFile(upLoad);
			applicationTemplate.setPath(path);
		}

		applicationTemplateService.update(applicationTemplate);

		return "tolist";
	}

	public String addUI() throws Exception {
		List<ProcessDefinition> processDefinitionList = processDefinitionService.findAllLatestVersions();
		ActionContext.getContext().put("processDefinitionList", processDefinitionList);

		return "saveUI";
	}

	public String add() throws Exception {
		String path=saveUploadFile(upLoad);
		model.setPath(path);
		applicationTemplateService.save(model);
		return "tolist";
	}

	public String downLoad() throws Exception {
        System.out.println(model.getId()+"-----------");
		ApplicationTemplate applicationTemplate = applicationTemplateService.getById(model.getId());
		inputStream = new FileInputStream(applicationTemplate.getPath());

		String fileName = URLEncoder.encode(applicationTemplate.getName(), "utf-8");
		ActionContext.getContext().put("fileName", fileName);
		System.out.println(fileName);

		return "downLoad";
	}

	// 得到数据
	public File getUpLoad() {
		return upLoad;
	}

	public void setUpLoad(File upLoad) {
		this.upLoad = upLoad;
	}

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public static void main(String[] args) {
		File file = new File("e:/test.txt");
		File file2 = new File("e:/Test/xiaowen.txt");
		file.renameTo(file2);
		System.out.println("========end==========");
	}
}
